package com.lishe.lishe.lishe.module.auth.view;

import android.content.Context;
import android.util.AttributeSet;

import com.lishe.lishe.lishe.base.view.FontAppCompatTextView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by zhcase on 2017/9/6.
 */

public class SendCodeTextView extends FontAppCompatTextView {


    private  int countDownMax = 60; //倒数秒数

    private int mTempCountDown = 0;

    private Timer mCountDownTimer = null;
    private TimerTask mCountDownTimerTask = null;

    private onSendCodeStateListener mOnSendCodeStateListener = null;

    public SendCodeTextView(Context context) {
        super(context);
    }

    public SendCodeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SendCodeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setCountDownMax(int secs)
    {
        countDownMax = secs;
    }

    public void startCountDown() {

        stopCountDown();

        if(mOnSendCodeStateListener != null)
        {
            mOnSendCodeStateListener.onSendCodeStart();
        }

        mTempCountDown = countDownMax;

        mCountDownTimer = new Timer();
        mCountDownTimerTask = new TimerTask() {
            @Override
            public void run() {
                mTempCountDown--;

                post(new Runnable() {
                    @Override
                    public void run() {

                        if(mTempCountDown>0)
                        {

                            setText(mTempCountDown+"s");
                            setClickable(false);
                        }else
                        {

                            stopCountDown();
                            setText("重新发送");
                            setClickable(true);
                            mTempCountDown = 60;

                            if(mOnSendCodeStateListener != null)
                            {
                                mOnSendCodeStateListener.onSendCodeStop();
                            }
                        }

                    }
                });
            }
        };

        mCountDownTimer.schedule(mCountDownTimerTask,0,1000);


    }

    public void prepareReSendCode()
    {
        stopCountDown();
        setText("重新发送");
        setClickable(true);
        mTempCountDown = 60;
    }

    public void stopCountDown() {

        if (mCountDownTimerTask != null) {
            mCountDownTimerTask.cancel();
            mCountDownTimerTask = null;
        }

        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }

    public void setOnSendCodeStateListener(onSendCodeStateListener listener)
    {
        mOnSendCodeStateListener = listener;
    }


    public interface onSendCodeStateListener
    {
        public void onSendCodeStart();

        public void onSendCodeStop();


    }



}
